-
The shell maintains a body of information during our shell session called the environment.
-
Data stored in the environment is used by programs to determine facts about our configuration.
-
While most programs use configuration files to store program settings, some programs will also look for values stored in the environment to adjust their behavior. Knowing this, we can use the environment to customize our shell experience.
-
Environment Variables.
-
Shell Variables.
-
Aliases.
-
Functions.
- You can think of the variable as a location that hold some information needed by a program to adjust its behavior.
Variable | Meaning | To view variable value type |
---|---|---|
BASH_VERSION | Holds the version of this instance of bash. | echo $BASH_VERSION |
HOSTNAME | The name of the your computer. | echo $HOSTNAME |
CDPATH | The search path for the cd command. | echo $CDPATH |
HISTFILE | The name of the file in which command history is saved. | echo $HISTFILE |
HISTFILESIZE | The maximum number of lines contained in the history file. | echo $HISTFILESIZE |
HISTSIZE | The number of commands to remember in the command history. The default value is 500. | echo $HISTSIZE |
HOME | The home directory of the current user. | echo $HOME |
LANG | Used to determine the locale category for any category not specifically selected with a variable starting with LC_. | echo $LANG |
PATH | The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. | echo $PATH |
PS1 | Your prompt settings. | echo $PS1 |
TERM | Your login terminal type. | echo $TERM export TERM=vt100 |
SHELL | Set path to login shell. | echo $SHELL |
EDITOR | Set name of default text editor. | export EDITOR=/usr/bin/vim |
-
Environment variables:
-
are system wide and are inherited by all system processes and shells.
-
Long Term Usage.
-
-
Shell variables:
-
only apply internally to the current shell instance.
-
Short Term Usage.
-
-
Their difference is similar to the difference between private fields and protected fields in a Java class.
-
The private fields of a Java class is only accessible from that Java class. The protected fields of a Java class is accessible from both that Java class and its subclasses.
-
The shell variables of a shell is only accessible from that shell process. The environment variables exported from that shell is accessible from both that shell process and the sub-processes created from that shell.
-
- To list all environment variables with their values write
printenv
without any arguments:
- To list the value of a specific environment variable write
printenv
followed by the variable name:
- To list all environment variables and all shell variables write
set
without any arguments:
-
You need to
export
,making it inheritable by child processes, this shell variable. -
Example:
- the behavior of a shell variable before exporting it
- the behavior of a shell variable after exporting it
- Alias is like a shortcut command which will have same functionality as if we are writing the whole command.
- write
alias
followed bynew command
followed by='original command'
- To list all aliases write
alias
without any arguments
- To show to value of a specific alias write
alias
followed byalias name
-
A function is simply a “chunk” of code that you can use over and over again, rather than writing it out multiple times.
-
Functions enable programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task.
-
You can create functions in different ways one of them is to write the keyword
function
followed byfunction-name()
followed by{ your code ;}
-
In order for a function to perform the code inside it it needs to be called by just write the name of the function.
-
Look at the following example:
When we log on to the system, the bash program starts, reads and execute a series of configuration scripts called startup files, which define the default environment shared by all users.
This is followed by more startup files in our home directory that define our personal environment and the exact sequence depends on the type of shell session being started.
There are two kinds:
-
Interactive Login Shell Session.
-
Interactive Non-login Shell Session.
Interactive
- Interactive means that the commands are run with user-interaction from keyboard. E.g. the shell can prompt the user to enter input.
Non-interactive
- the shell is probably run from an automated process so it can't assume it can request input or that someone will see the output. E.g., maybe it is best to write output to a log file.
- An interactive shell can be either login or non-login shell.
So...
- An interactive login shell is invoked when a user login to the terminal either remotely via ssh or locally, or when Bash is launched with the --login option.
- An interactive non-login shell is invoked from the login shell, such as when typing bash in the shell prompt or when opening a new terminal tab.
When bash is invoked as Interactive Login Shell, bash looks for the following files in order:
-
/etc/profile
- A global configuration script that applies to all users. -
~/.bash_profile
- A user's personal startup file. It can be used to extend of override settings in the global configuration script. -
~/.bash_login
- If ~/.bash_profile is not found, bash attempts to read this file. -
~/.profile
- If neither ~/.bash_profile nor ~/.bash_login is found, bash attempts to read this file.- This is the default file in Debian-based distributions, such as Ubuntu.
When bash is invoked as Interactive Non-Login Shell, bash looks for the following files in order:
-
/etc/bash.bashrc
- A global configuration script that applies to all users. -
~/.bashrc
- A user's personal startup file. It can be used to extend or override settings in the global configuration script.
In addition to reading the startup files above, non-login shells also inherit the environment from their parent process, usually a login shell.
- if the output of the
$0
variable is
then this shell is a login shell.
- if the output of the
$0
variable is
then this shell is a non login shell.
-
Put the commands that should run every time you launch a new shell in the
~/.bashrc
file. This include your aliases, functions, and shell variables.- you can add aliases to
~/.bashrc
as following:
- you can add functions to
~/.bashrc
as following:
- you can add shell variables to
~/.bashrc
as following:
- you can add aliases to
-
Use .bash_profile to run commands that should run only once, such as environment variables.
- you can add environment variables to
~/.bash_profile
as following:
- you can add environment variables to
-
You can just write
source
following byfile path
-
We can use this to organize our .bashrc file by for example put our alias in different file and source(include) it in .bashrc
-
You can put your aliases in different file and source it in .bashrc as folllowing:
- You can put your functions in different file and source it in .bashrc as folllowing:
NOTE: Functions in the previous figure are written in different valid syntax than the previous one we created before.